package com.wisdytech.linkdcs.equipLifeCycle.check.service.impl;

import java.util.Date;
import java.util.List;

import com.wisdytech.common.utils.PojoConvertUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.dao.CheckModelItemBakDao;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckModelItemBak;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceItem;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceItemBak;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.dao.ICheckDetailRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.check.dao.ICheckModelItemDao;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckModelItem;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckModelItemService;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckService;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dailibo
 * @since 2018-06-05
 */
@Service
@Transactional
public class CheckModelItemServiceImpl extends BaseService<ICheckModelItemDao, CheckModelItem> implements ICheckModelItemService {

	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ICheckModelItemDao iCheckModelItemDao;

    @Autowired
    private CheckModelItemBakDao checkModelItemBakDao;
	
	@Autowired
	private ICheckService iCheckService;
	
	@Override
	public boolean add(CheckModelItem item,String createUser) {
		
		//判断点检编码是否唯一
		EntityWrapper<CheckModelItem> ew =new EntityWrapper<>();
		Integer count =iCheckModelItemDao.selectCount(
				ew.eq("CHECK_ITEM_CODE", item.getCheckItemCode())
				.eq("DEL_FLAG", "0"));
		if(count>0) {
			throw new ServiceException("点检项目编码已存在");
		}
        String pkId = BaseUtil.getUUID();
        String snapshotId = BaseUtil.getUUID();
        item.setPkId(pkId);
		item.setDelFlag("0");
		item.setCreateUser(createUser);
		item.setCreateDate(new Date());
		item.setUpdateDate(new Date());
		item.setVersion(snapshotId);
		if(iCheckModelItemDao.insert(item)<=0) {
			throw new ServiceException("保存点检项目失败");
		}
        CheckModelItem snapshot = iCheckModelItemDao.selectById(pkId);
        CheckModelItemBak mItem = PojoConvertUtil.convertPojo(snapshot, CheckModelItemBak.class);
        mItem.setDelFlag(CommonConstants.DEL_FLAG);
        mItem.setPkId(snapshotId);
        if(checkModelItemBakDao.insert(mItem)<=0) {
            throw new ServiceException("保存快照失败");
        }
		// 保存点检项目后，点检管理中增加一条点检记录
		return iCheckService.addCheckRecordByModel(item.getModelCode(),createUser);
	}

	@Override
	public boolean update(CheckModelItem item,String udpateUser) {
		
		CheckModelItem oldItem =
				iCheckModelItemDao.selectById(item.getPkId());
		//编码改变，查看编码是否唯一
		if(!oldItem.getCheckItemCode().equals(item.getCheckItemCode())) {
			//判断点检编码是否唯一
			EntityWrapper<CheckModelItem> ew =new EntityWrapper<>();
			Integer count =iCheckModelItemDao.selectCount(
					ew.eq("CHECK_ITEM_CODE", item.getCheckItemCode())
					.eq("DEL_FLAG", CommonConstants.DEL_FLAG));
			if(count>0) {
				throw new ServiceException("点检项目编码已存在");
			}
		}
		
		//更新
        String snapshotId = BaseUtil.getUUID();
		item.setUpdateUser(udpateUser);
		item.setUpdateDate(new Date());
        item.setVersion(snapshotId);
		if(iCheckModelItemDao.updateById(item)<=0) {
			throw new ServiceException("更新点检项目失败");
		}
        CheckModelItem snapshot = iCheckModelItemDao.selectById(item.getPkId());
        CheckModelItemBak mItem = PojoConvertUtil.convertPojo(snapshot, CheckModelItemBak.class);
        mItem.setDelFlag(CommonConstants.DEL_FLAG);
        mItem.setPkId(snapshotId);
        if(checkModelItemBakDao.insert(mItem)<=0) {
            throw new ServiceException("保存快照失败");
        }
		return true;
	}

	@Override
	public boolean del(String itemIds,String createUser) {
		String[] itemIdList=
				itemIds.split(",");
		
		CheckModelItem  itemModel =
				iCheckModelItemDao.selectById(itemIdList[0]);
		
		for(String itemId : itemIdList) {
			CheckModelItem item =new CheckModelItem();
			item.setPkId(itemId);
			item.setUpdateUser(createUser);
			item.setUpdateDate(new Date());
			item.setDelFlag(CommonConstants.DEL_FLAG_TRUE);
			if(!item.updateById()) {
				throw new ServiceException("删除点检项目失败");
			}
		}
		
		//判断设备型号下是否还有点检项目
		if(iCheckModelItemDao.selectCount(
				new EntityWrapper<CheckModelItem>()
					.eq("DEL_FLAG", CommonConstants.DEL_FLAG)
					.eq("MODEL_CODE", itemModel.getModelCode()))<=0) {
			//根据点检项目id判断是否修改点检管理
			return iCheckService.delCheckRecordByModel(itemModel.getModelCode(),createUser);
		}
		return true;
	}

	@Override
	public PageInfo<CheckModelItem> listCheckModelItems(Filter filter, String modelCode) {
		
		EntityWrapper<CheckModelItem> ew=new EntityWrapper<>();
		List<CheckModelItem>  lists =this.beforeList(ICheckModelItemDao.class, filter)
			.selectList(ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG)
					.eq("MODEL_CODE", modelCode));
		
		return new PageInfo<CheckModelItem>(lists);
	}

	@Override
	public PageInfo<CheckModelItem> listCheckModelItemsByEquip(Filter filter, String checkId) {
		
		
		List<CheckModelItem>  reuslt =this.beforeList(ICheckModelItemDao.class, filter)
			.selectDetailByCheckId(checkId);
		
		return new PageInfo<CheckModelItem>(reuslt);
	}

	@Override
	public CheckModelItem getItemById(String pkId) {
		// TODO Auto-generated method stub
		return iCheckModelItemDao.selectById(pkId);
	}

	

}
